<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }

        span {
            /* 动画一定要加定位 */
            position: absolute;
            left: 0;
            top: 200px;
            display: block;
            width: 150px;
            height: 150px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <button id='btn'>开启500</button>
    <div>一路向北</div>
    <script>
        /* 
            需求：点击按钮，div 移动，移动速度逐渐变慢，直到降为 0

            步长取整: 步长小于 1 px，就会出现各种问题。
        */

        let btn = document.querySelector('button')
        let div = document.querySelector('div')

        btn.addEventListener('click', function () {
            animate(div, 400)
        });

        function animate(obj, target) {
            clearInterval(obj.timer)
            obj.timer = setInterval(() => {
                let left = obj.offsetLeft;
                if (left > target) {
                    clearInterval(obj.timer)
                    return;
                }
                //  步长 = （目标位置 - 当前位置） / 10（随意数）
                let step = (target - left) / 10;

                step = Math.ceil(step);
                obj.style.left = left + step + 'px';
            }, 100);
        }
    </script>

</body>

</html>